package com.jinke.api.modules.ums.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.ObjectMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.jinke.api.common.api.CommonPage;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.generator.GsonUtil;
import com.jinke.api.modules.app.controller.StaticController;
import com.jinke.api.modules.app.model.BalanceLog;
import com.jinke.api.modules.app.request.BalanceLogQueryRequest;
import com.jinke.api.modules.app.response.BalanceLogExport;
import com.jinke.api.modules.app.service.BalanceLogService;
import com.jinke.api.modules.ums.vo.SimpleUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-13
 */
@RestController
@RequestMapping("/admin/balance/log")
@Api(value = "后台资金明细", tags = "后台资金明细")
public class UmsBalanceLogController {

    @Resource
    BalanceLogService balanceLogService;
    @Resource
    private StaticController staticController;//down下载需要用到的
    @ApiOperation("财务管理-用户资金明细")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<BalanceLog>> list(BalanceLogQueryRequest request) {
            //CommonResult通用返回对象<CommonPage分页数据封装类<封装数据库类>>  List返回列表（请求类）
            //搜索需要开始日期，结束日期，钱包类型（保证金余额），资金类型（充值或者消费） ，根据邮箱进行搜索，根据订单号回传页面显示
        //request.setUserId(AdminUserUtil.getUserId().intValue());
        Page<BalanceLog> list = balanceLogService.list(request);
            //page分页<数据库类> list = 资金明细接口的list(返回请求)
        return CommonResult.success(CommonPage.restPage(list));
            //分页请求成功返回 结果.success（获取的数据是，所有数据分页CommonPage的.restPage（传回刚才请求的list  rest进分页））
    }

    @ApiOperation("财务管理-用户资金明细")
    @RequestMapping(value = "/down", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<String> down(BalanceLogQueryRequest request) {
        //为了一次性获取足够多的数据，而不必担心分页限制
        request.setPageSize(999999);
        request.setPageNum(1);
        Page<BalanceLog> list = balanceLogService.list(request);
        List<BalanceLog> records = list.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<BalanceLogExport> balanceLogExports = records.stream().map(x -> {
                BalanceLogExport userRechargeExport = new BalanceLogExport();
                BeanUtil.copy(x, userRechargeExport);
                userRechargeExport.setType(x.getType().getDesc());
                userRechargeExport.setBalanceType(x.getBalanceType().getDesc());
                if (StringUtils.isNotEmpty(x.getDetails())) {
                    userRechargeExport.setDetails(x.getDetails().toString());
                }


                SimpleUser user = x.getUser();
                if(user != null){
                    userRechargeExport.setEmail(user.getEmail());
                }
                return userRechargeExport;

            }).collect(Collectors.toList());
            String s = staticController.exports(balanceLogExports, BalanceLogExport.class);
            return CommonResult.success(s);
        }
        return CommonResult.success();
    }

}

